
<Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> Friend Class Scale

    Private m_Address As Integer = -1
    Private m_Multiplier As Double = 1
    Private m_Overlay() As String = Nothing
    Private m_trace As traceScheme = traceScheme.TRC_NONE
    Private m_Offset As Double = 0
    Private m_PreDivider As Double = 0
    Private m_Description As BikDescription = Nothing

    '' These two variables are used to determine min and max points for the text overlay of a scale
    '' Predominatley in the BikiROm Tables configurations.
    Private m_minVar As Var = Nothing
    Private m_maxVar As Var = Nothing

    Sub New()
    End Sub
    Sub New(ByVal address As Integer, ByVal multiplier As Double, ByVal Offset As Double, ByVal preDivider As Double, ByVal Description As BikDescription)
        m_Address = address
        m_Multiplier = multiplier
        m_Offset = Offset
        m_PreDivider = preDivider
        m_Description = Description
    End Sub
    Sub New(ByVal multiplier As Double, ByVal varMin As Var, ByVal varMax As Var, ByVal trace As BikSettings.TableVariables)
        m_Multiplier = multiplier
        m_minVar = varMin
        m_maxVar = varMax
        m_trace = BikSettings.TableVariableTrace(trace)
    End Sub

    Public Property Trace() As traceScheme
        Get
            Return m_trace
        End Get
        Set(ByVal value As traceScheme)
            m_trace = value
        End Set
    End Property
    Public Property Address() As Integer
        Get
            Return m_Address
        End Get
        Set(ByVal value As Integer)
            m_Address = Address
        End Set
    End Property
    Public Property Multiplier() As Double
        Get
            Return m_Multiplier
        End Get
        Set(ByVal value As Double)
            m_Multiplier = value
        End Set
    End Property
    Public Property Offset() As Double
        Get
            Return m_Offset
        End Get
        Set(ByVal value As Double)
            m_Offset = value
        End Set
    End Property
    Public Property Overlay() As String()
        Get
            Return m_Overlay
        End Get
        Set(ByVal value As String())
            m_Overlay = value
        End Set
    End Property
    Public Property preDivider() As Double
        Get
            Return m_PreDivider
        End Get
        Set(ByVal value As Double)
            m_PreDivider = value
        End Set
    End Property
    Public Property Description() As BikDescription
        Get
            Return m_Description
        End Get
        Set(ByVal value As BikDescription)
            m_Description = value
        End Set
    End Property
    Public Property minVar() As Var
        Get
            Return m_minVar
        End Get
        Set(ByVal value As Var)
            m_minVar = value
        End Set
    End Property
    Public Property maxVar() As Var
        Get
            Return m_maxVar
        End Get
        Set(ByVal value As Var)
            m_maxVar = value
        End Set
    End Property

    Shared Operator =(ByVal map1 As Scale, ByVal map2 As Scale) As Boolean
        If Not IsNothing(map1) And Not IsNothing(map2) Then
            If map1.Address >= 0 And map2.Address >= 0 Then
                If map1.Address = map2.Address Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        Else
            If IsNothing(map2) And IsNothing(map1) Then
                Return False
            ElseIf Not IsNothing(map1) Or Not IsNothing(map2) Then
                Return False
            Else
                Return False
            End If
        End If
    End Operator
    Shared Operator <>(ByVal map1 As Scale, ByVal map2 As Scale) As Boolean
        If map1 = map2 Then
            Return False
        Else
            Return True
        End If
    End Operator

    Friend Function getRaw(ByRef array As Byte(), ByVal length As Short) As Byte()
        If m_Address <> -1 And length >= 1 Then
            Dim f_return(length - 1) As Byte
            For i As Int16 = 0 To length - 1
                f_return(i) = array(m_Address + i)
            Next
            Return f_return
        Else
            Return Nothing
        End If
    End Function
    Friend Sub setRaw(ByRef array As Byte(), ByVal setArray As Byte())
        If m_Address <> -1 And setArray.Length >= 1 Then
            For i As Int16 = 0 To UBound(setArray)
                array(m_Address + i) = setArray(i)
            Next
        End If
    End Sub
    Friend Function getValues(ByRef Array As Byte(), ByVal m_length As Short) As Single()
        If m_Address <> -1 Then
            Dim f_return(m_length - 1) As Single
            For i As Integer = 0 To m_length - 1
                f_return(i) = (Array(m_Address + i) * m_Multiplier) + m_Offset
            Next
            Return f_return
        ElseIf Not m_Overlay Is Nothing AndAlso m_Overlay.Length = m_length Then
            Dim f_return(m_length - 1) As Single
            For i As Integer = 0 To m_length - 1
                f_return(i) = Val(m_Overlay(i))
            Next
            Return f_return
        Else
            Dim f_return(m_length - 1) As Single
            For i As Integer = 0 To m_length - 1
                f_return(i) = i + 1
            Next
            Return f_return
        End If
    End Function
    Friend Sub setValues(ByRef array As Byte(), ByRef setArray As Double())
        If Not IsNothing(setArray) Then
            For i As Int16 = 0 To UBound(setArray)
                array(m_Address + i) = (setArray(i) - m_Offset) / m_Multiplier
            Next
        End If
    End Sub
    Friend Sub MultiplyValues(ByRef array As Byte(), ByVal Multi As Double, ByVal length As Integer)
        If m_Address >= 0 And length >= 1 Then
            For i As Integer = 0 To length - 1
                array(m_Address + i) = iByte(array(m_Address + i) * Multi)
            Next
        End If
    End Sub

End Class
